package com.fanrenqiji.modules.sys.entity;

import com.fanrenqiji.common.persistence.DataEntity;
import com.google.common.collect.Lists;
import org.hibernate.annotations.*;
import org.hibernate.validator.constraints.Length;

import javax.persistence.CascadeType;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.beans.Transient;
import java.util.List;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年1月19日 上午10:24:27
 * @description
 */
@Entity
@Table(name = "sys_menu")
@DynamicInsert
@DynamicUpdate
public class Menu extends DataEntity {

	private static final long serialVersionUID = 1L;

	public Menu() {
		super();
		this.sort = 30;
	}

	public Menu(Long id) {
		this();
		this.id = id;
	}

	/**
	 * 唯一识别编号
	 */
	private Long id;
	/**
	 * 父级菜单
	 */
	private Menu parent;
	/**
	 * 记录所有顺延的父级编号，通过当前的编号获得级别的深度。
	 */
	private String parentIds;
	/**
	 * 菜单名称
	 */
	private String name;
	/**
	 * 资源链接
	 */
	private String href;
	/**
	 * 目标是否是重新打卡（ mainFrame、_blank、_self、_parent、_top）
	 */
	private String target;
	/**
	 * 简单显示的icon图标
	 */
	private String icon;
	/**
	 * 排序
	 */
	private Integer sort;
	/**
	 * 是否在菜单中显示（1：显示；0：不显示）
	 */
	private String isShow;
	/**
	 * 权限标识记录有没有编辑和查看的权限
	 */
	private String permission;
	/**
	 * 拥有子菜单列表
	 */
	private List<Menu> childList = Lists.newArrayList();
	/**
	 * 拥有角色列表
	 */
	private List<Role> roleList = Lists.newArrayList();

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "parent_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@NotNull
	public Menu getParent() {
		return parent;
	}

	public void setParent(Menu parent) {
		this.parent = parent;
	}

	@Length(min = 1, max = 255)
	@Column(name = "parent_ids")
	public String getParentIds() {
		return parentIds;
	}

	public void setParentIds(String parentIds) {
		this.parentIds = parentIds;
	}

	@Length(min = 1, max = 100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Length(min = 0, max = 255)
	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	@Length(min = 0, max = 20)
	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	@Length(min = 0, max = 100)
	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	@NotNull
	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	@Length(min = 1, max = 1)
	@Column(name = "is_show")
	public String getIsShow() {
		return isShow;
	}

	public void setIsShow(String isShow) {
		this.isShow = isShow;
	}

	@Length(min = 0, max = 200)
	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
			CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "parent")
	@Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
	@OrderBy(value = "sort")
	@NotFound(action = NotFoundAction.IGNORE)
	public List<Menu> getChildList() {
		return childList;
	}

	public void setChildList(List<Menu> childList) {
		this.childList = childList;
	}

	@ManyToMany(mappedBy = "menuList", fetch = FetchType.LAZY)
	@Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
	@OrderBy("id")
	@Fetch(FetchMode.SUBSELECT)
	@NotFound(action = NotFoundAction.IGNORE)
	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	/**
	 * 对菜单进行排序
	 * 
	 * @param list
	 * @param sourcelist
	 * @param parentId
	 */
	@Transient
	public static void sortList(List<Menu> list, List<Menu> sourcelist,
			long parentId) {
		for (int i = 0; i < sourcelist.size(); i++) {
			Menu e = sourcelist.get(i);
			if (e.getParent() != null && e.getParent().getId() != null
					&& e.getParent().getId().equals(parentId)) {
				list.add(e);
				// 判断是否还有子节点, 有则继续获取子节点
				for (int j = 0; j < sourcelist.size(); j++) {
					Menu child = sourcelist.get(j);
					if (child.getParent() != null
							&& child.getParent().getId() != null
							&& child.getParent().getId().equals(e.getId())) {
						sortList(list, sourcelist, e.getId());
						break;
					}
				}
			}
		}
	}

	/*
	 * @Transient public boolean isRoot(){ return isRoot(this.id);}
	 */

	@Transient
	public static boolean isRoot(Long id) {
		return id != null && id.equals(1L);
	}

}
